package com.example.myliwu;

import androidx.appcompat.app.AppCompatActivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.myliwu.fragment.BaseFragment;
import com.example.myliwu.fragment.HomeFragment;
import com.example.myliwu.fragment.ShequFragment;
import com.example.myliwu.fragment.WeihuFragment;
import com.example.myliwu.fragment.WodeFragment;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {

    private TextView mhome;
    private TextView weixiu;
    private TextView mweixiu;
    private TextView mshequ;
    private TextView mwode;
    private FragmentTransaction mfragmentTransaction;
    private ShequFragment mshequFragment;
    private WeihuFragment mweihuFragment;
    private WodeFragment mwodeFragment;
    private HomeFragment mhomeFragment;
    private FragmentManager fragmentManager;
    private FragmentManager mfragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        homeView();
    }


    private void homeView() {
        mhome = findViewById(R.id.xml_home);
        mweixiu = findViewById(R.id.xml_weixiu);
        mshequ = findViewById(R.id.xml_shequ);
        mwode = findViewById(R.id.xml_wode);

        //增加点击事件
        //找到相应的Relativelayout容器
        RelativeLayout tv_tb_home = findViewById(R.id.tv_tb_home);
        RelativeLayout tv_tb_weihu = findViewById(R.id.tv_tb_weihu);
        RelativeLayout tv_tb_shequ = findViewById(R.id.tv_tb_shequ);
        RelativeLayout tv_tb_wode = findViewById(R.id.tv_tb_wode);
        //默认选中状态
        mhome.setBackgroundResource(R.drawable.ic_baseline_home);
        //设置监听器
        tv_tb_home.setOnClickListener(this);
        tv_tb_weihu.setOnClickListener(this);
        tv_tb_shequ.setOnClickListener(this);
        tv_tb_wode.setOnClickListener(this);
        //创建Fragment对象
        mhomeFragment = new HomeFragment();
        //关联起来 在MaiActvity管理Fragment 强耦合 设计中间类 管理
        mfragmentManager = getFragmentManager();
        mfragmentTransaction = mfragmentManager.beginTransaction();
        //注意导包 fragment
        mfragmentTransaction.replace(R.id.content_layout, mhomeFragment);
        //事务内部替换
        //提交后才是替换
        mfragmentTransaction.commit();


    }

    @Override
    public void onClick(View view) {
        //事务不能和其他事务混用
        //点击谁 系统默认把它传进来
        FragmentTransaction fragmentTransaction = mfragmentManager.beginTransaction();
        int id = view.getId();
        switch (id) {
            //当点击home执行
            case R.id.tv_tb_home:
                System.out.println("hom被点击");
                //设置为填充颜色图标
                //todo 切换到Home页面
                //如果不存在就创建，存在直接show出来
                //隐藏除Home页面以外的页面
                if (mweihuFragment != null) {
                    fragmentTransaction.hide(mweihuFragment);
                    fragmentTransaction.addToBackStack(null);
                }

                if (mshequFragment != null) {
                    fragmentTransaction.hide(mshequFragment);
                }
                if (mwodeFragment != null) {
                    fragmentTransaction.hide(mwodeFragment);
                }

                if (mhomeFragment == null) {
                    mhomeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.content_layout, mhomeFragment);
                } else {
                    fragmentTransaction.show(mhomeFragment);
                }



                changColors(mhome, R.drawable.ic_baseline_home);
                break;
            //当点击weihu执行
            case R.id.tv_tb_weihu:
                System.out.println("weihu被点击");
                //设置为填充颜色图标
                //todo 切换到社区页面
                //先把HomeFragment隐藏掉
                //隐藏除维护页面以外的页面
                if (mhomeFragment != null) {
                    fragmentTransaction.hide(mhomeFragment);
                }
                if (mshequFragment != null) {
                    fragmentTransaction.hide(mshequFragment);
                }
                if (mwodeFragment != null) {
                    fragmentTransaction.hide(mwodeFragment);
                }


                if (mweihuFragment == null) {
                    mweihuFragment = new WeihuFragment();
                    fragmentTransaction.add(R.id.content_layout, mweihuFragment);
                } else {
                    fragmentTransaction.show(mweihuFragment);
                }

                changColors(mweixiu, R.drawable.ic_baseline_weihu);
                break;
            case R.id.tv_tb_shequ:
                System.out.println("shequ被点击");
                //设置为填充颜色图标
                //todo 切换到社区页面
                //隐藏除社区页面以外的页面
                if (mhomeFragment != null) {
                    fragmentTransaction.hide(mhomeFragment);
                }
                if (mweihuFragment != null) {
                    fragmentTransaction.hide(mweihuFragment);
                }
                if (mwodeFragment != null) {
                    fragmentTransaction.hide(mwodeFragment);
                }


                if (mshequFragment == null) {
                    mshequFragment = new ShequFragment();
                    fragmentTransaction.add(R.id.content_layout, mshequFragment);
                } else {
                    fragmentTransaction.show(mshequFragment);
                }

                changColors(mshequ, R.drawable.ic_baseline_shequ);
                break;
            case R.id.tv_tb_wode:
                System.out.println("wode被点击");
                //todo 切换到我的主页
                //隐藏除我的主页页面以外的页面

                if (mhomeFragment != null) {
                    fragmentTransaction.hide(mhomeFragment);
                }

                if (mweihuFragment != null) {
                    fragmentTransaction.hide(mweihuFragment);
                }
                //隐藏社区页面
                if (mshequFragment != null) {
                    fragmentTransaction.hide(mshequFragment);
                }


                if (mwodeFragment == null) {
                    mwodeFragment = new WodeFragment();
                    fragmentTransaction.add(R.id.content_layout, mwodeFragment);
                } else {
                    fragmentTransaction.show(mwodeFragment);
                }
                changColors(mwode, R.drawable.ic_baseline_wode);
                break;
        }
        fragmentTransaction.commit();
    }


    private void changColors(TextView changeTextView, int changeId) {
        //把所有的设置为白色选中状态
        mhome.setBackgroundResource(R.drawable.ic_baseline_home2);
        mweixiu.setBackgroundResource(R.drawable.ic_baseline_weihu2);
        mshequ.setBackgroundResource(R.drawable.ic_baseline_shequ2);
        mwode.setBackgroundResource(R.drawable.ic_baseline_wode2);

        //把选中的设置为黄色
        changeTextView.setBackgroundResource(changeId);
    }


}